package com.example.recuitactivity.myMainActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.R;
import com.example.recuitactivity.RegisterActivity;
import com.example.recuitactivity.helper.DbOpenHelper;

public class MainActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private TextView tv_username_check;
    private TextView tv_password_check;
    private RadioGroup radioGroup;
    private TextView tv_register;
    private Button btn_login;
    private DbOpenHelper dbOpenHelper;

    private int status=0;//求职者:0,企业：1，管理员：2

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.btn_login:
                    login();
                    break;
                case R.id.tv_toregister:
                    Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener listener2=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(i==R.id.radioButton1){
                status=0;
            }else if(i==R.id.radioButton2){
                status=1;
            }else{
                status=2;
            }
        }
    };


    public void initUI(){
        et_username=(EditText) findViewById(R.id.et_username_login);
        et_password=(EditText) findViewById(R.id.et_password_login);
        tv_username_check=(TextView) findViewById(R.id.tv_username_login_check);
        tv_password_check=(TextView) findViewById(R.id.tv_pwd_login_check);
        radioGroup=(RadioGroup) findViewById(R.id.radiogroup);
        tv_register=(TextView) findViewById(R.id.tv_toregister);
        btn_login=(Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(listener);
        tv_register.setOnClickListener(listener);
        radioGroup.setOnCheckedChangeListener(listener2);

        dbOpenHelper=new DbOpenHelper(MainActivity.this);;
    }

    public void login(){
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();//测试数据


        String username = et_username.getText().toString();
        String password = et_password.getText().toString();
        boolean login_successs = true;
        tv_username_check.setText("");
        tv_password_check.setText("");
        Cursor cursor=null;
        switch(status){
            case 0:
                cursor=db.query("candidates",new String[]{"cpassword"},"cusername=?",new String[]{username},null,null,null);
                if(username==null||username==""){
                    login_successs=false;
                    tv_username_check.setText("用户名不能为空");
                }else if(cursor.moveToFirst()){
                    int pwd_index=cursor.getColumnIndex("cpassword");
                    String pwd=cursor.getString(pwd_index);
                    if(password==null||password==""){
                        login_successs=false;
                        tv_password_check.setText("密码不能为空");
                    } else if(!password.equals(pwd)){
                        login_successs=false;
                        tv_password_check.setText("密码错误");
                    }
                }else{
                    login_successs=false;
                    tv_username_check.setText("用户名错误");
                }
                if(login_successs){
                    Intent intent=new Intent(MainActivity.this, CandidateMainActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case 1:
                cursor=db.query("enterprises",new String[]{"epassword"},"eusername=?",new String[]{username},null,null,null);
                if(username==null||username==""){
                    login_successs=false;
                    tv_username_check.setText("用户名不能为空");
                }else if(cursor.moveToFirst()){
                    int pwd_index=cursor.getColumnIndex("epassword");
                    String pwd=cursor.getString(pwd_index);
                    if(password==null||password==""){
                        login_successs=false;
                        tv_password_check.setText("密码不能为空");
                    } else if(!password.equals(pwd)){
                        login_successs=false;
                        tv_password_check.setText("密码错误");
                    }
                }else{
                    login_successs=false;
                    tv_username_check.setText("用户名错误");
                }
                if(login_successs){
                    Intent intent=new Intent(MainActivity.this, EnterpriseMainActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                cursor=db.query("admins",new String[]{"apassword"},"ausername=?",new String[]{username},null,null,null);
                if(username==null||username==""){
                    login_successs=false;
                    tv_username_check.setText("用户名不能为空");
                }else if(cursor.moveToFirst()){
                    int pwd_index=cursor.getColumnIndex("apassword");
                    String pwd=cursor.getString(pwd_index);
                    if(password==null||password==""){
                        login_successs=false;
                        tv_password_check.setText("密码不能为空");
                    } else if(!password.equals(pwd)){
                        login_successs=false;
                        tv_password_check.setText("密码错误");
                    }
                }else{
                    login_successs=false;
                    tv_username_check.setText("用户名错误");
                }
                if(login_successs){
                    Intent intent=new Intent(MainActivity.this, AdminMainActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
        cursor.close();

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //让键盘弹出来是，内容不整体上移
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        initUI();
    }

}